<!--
 * @Author: blak-kong
 * @Date: 2019-12-11 15:39:06
 * @Email: 546598185@qq.com
 -->
<template>
    <div class="slideshow">
        <el-button type="primary" icon="el-icon-plus" @click="add">新增轮播图</el-button>
        <el-table
            :data="tableData"
            border
            style="width: 100%;margin-top:10px;">
            <el-table-column
                label="名称"
                width="200">
                <template slot-scope="scope">
                    <el-popover trigger="hover" placement="top">
                        <p>点击进行编辑</p>
                        <div slot="reference" class="name-wrapper">
                            <p contenteditable>{{scope.row.name}}</p>
                        </div>
                    </el-popover>
                </template>
            </el-table-column>
            <el-table-column
                label="链接地址">
                 <template slot-scope="scope">
                     <el-popover trigger="hover" placement="top">
                        <p>点击进行编辑</p>
                        <div slot="reference" class="name-wrapper">
                            <p contenteditable>{{scope.row.url}}</p>
                        </div>
                    </el-popover>
                 </template>
            </el-table-column>
            <el-table-column
                label="图片">
                <template slot-scope="scope">
                    <el-upload
                        @click="tableIndex = scope.row.$index"
                        class="avatar-uploader"
                        :action="process.env.VUE_APP_AJAX + '/api/upload'"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                        :before-upload="beforeAvatarUpload">
                        <img v-if="scope.row.img" :src="scope.row.img" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </template>
            </el-table-column>
             <el-table-column
                label="编辑">
                <template slot-scope="scope">
                    <div style="display:flex;">
                        <el-button size="mini" @click="handleSave(scope.row)">保存</el-button>
                        <el-button size="mini" @click="handleDelete(scope.row, scope.row.$index)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
import { imageType } from '../utils/mUtils'

export default {
    data() {
         return {
            tableIndex: 0,
            imgType:  '',
            imageUrl: '',
            tableData: []
         }
    },

     mounted() {
         this.getSlideshow()
     },

    methods: {
        add() {
            this.tableData.push({
                name: "点击修改",
                url: "sprout-edu.com",
                img: ""
            })
        },
        addData(item) {
            console.log(item)
             this.$axios.post(`/api/slideshow/add`, item).then(res => {
                if (res.data.errno === 0) {
                     this.$message({
                        message: res.data.msg,
                        type: "success"
                    })
                    return
                }
                this.$message(res.data.msg)
            })
        },
        updateData(item) {
             this.$axios.post(`/api/slideshow/update/${item._id}`, item).then(res => {
                if (res.data.errno === 0) {
                     this.$message({
                        message: res.data.msg,
                        type: "success"
                    })
                    return
                }
                this.$message(res.data.msg)
            })
        },
        deleteData(id,index) {
             this.$axios.post(`/api/slideshow/delete/${id}`, this.tableData).then(res => {
                if (res.data.errno === 0) {
                     this.$message({
                        message: res.data.msg,
                        type: "success"
                    })
                    this.deleteRow(index)
                    return
                }
                this.$message(res.data.msg)
            })
        },
        deleteRow(index) {
             this.tableData.splice(index, 1)
        },
        handleSave(item) {
            if (item._id) {
                this.updateData(item)
            } else {
                this.addData(item)
            }
        },
        handleDelete(item, index) {
            if (item._id) {
                this.deleteData(item._id, index)
            } else {
                this.deleteRow(index)
            }
        },

        getSlideshow() {
            this.$axios('/api/slideshow').then(res => {
                if (res.data.errno === 0)  {
                    this.tableData = res.data.result
                }
                console.log(this.tableData)
            })
        },

        handleAvatarSuccess(res, file) {
            console.log(res, file)
            this.tableData[this.tableIndex].img = process.env.VUE_APP_AJAX + res.filename
            // this.tableData[this.tableIndex].img = URL.createObjectURL(file.raw);
        },

        beforeAvatarUpload(file) {
            const isIMG = imageType(file.type);
            const isLt2M = file.size / 1024 / 1024 < 2;

            this.imgType = isIMG
            if (!isIMG) {
                this.$message.error('文件格式错误!');
            }
            if (!isLt2M) {
                this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isIMG && isLt2M;
        },

    }
}
</script> 


<style lang="less" scoped>
.slideshow {
    padding: 20px;
}

.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 80px;
    height: 80px;
    line-height: 80px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 4px;
  }
  .avatar {
    width: 80px;
    height: 80px;
    display: block;
  }
</style>